package sdmakerz_v1.coresyntax.generator

import fr.lip6.move.care.CareFactory
import interfacegen.java.JavaInterfaceFactory
import java.util.ArrayList
import java.util.List
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.IFileSystemAccess
import org.eclipse.xtext.generator.IGenerator
import sdmakerz_v1.coresyntax.SDMakerV1RuntimeModule
import sdmakerz_v1.coresyntax.generator.interfaces.IContext
import sdmakerz_v1.coresyntax.generator.interfaces.IInputData
import sdmakerz_v1.coresyntax.generator.interfaces.ISDMakerFolderLevelGen
import sdmakerz_v1.coresyntax.generator.interfaces.ISDMakerGenerator
import sdmakerz_v1.coresyntax.generator.interfaces.ISDMakerModelLevelGen
import sdmakerz_v1.coresyntax.io.SDMakerIO

class SDMakerV1Generator implements IGenerator{
	private var List<ISDMakerGenerator> generators;
	
	public new(){
		generators= new ArrayList<ISDMakerGenerator>();
	}
	
	def public 
	void addGenerator(ISDMakerGenerator generator){
		generators.add(generator);
	}
	
	def public 
	void addAllGenerator(List<ISDMakerGenerator> generators){
		this.generators.addAll(generators)
	}
	
	override doGenerate(Resource input, IFileSystemAccess fsa) {
		var SDMakerIO sdmakerIO= new SDMakerIO();
        var List<IInputData> inputs= sdmakerIO.allInputs;
		var IContext context= 
			SDMakerV1RuntimeModule::bindIContext.newInstance;

		context.init();

		addGenerator(JavaInterfaceFactory::newJavaInterfaceGen);
		addGenerator(CareFactory::newCareGen);
		
		for(ISDMakerFolderLevelGen generator : 
			generators.filter(typeof(ISDMakerFolderLevelGen))){
				
				generator.doGenerate(context, fsa);
				context.cleanContext
			}
		
	    for(IInputData inputDTmp: inputs){
	    	for(ISDMakerModelLevelGen generator : 
				generators.filter(typeof(ISDMakerModelLevelGen))){

	    		if(generator.canHandle(inputDTmp.getInput)){
	    			context.setInput(inputDTmp);
	    			generator.doGenerate(context, fsa);
	    			context.cleanContext
	    		}
	    	}
		}
	}
}